package cn.sumpu.app.wlcity.web.mvc.content;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.sumpu.app.wlcity.biz.ContentBiz;
import cn.sumpu.app.wlcity.domain.Content;
import cn.sumpu.app.wlcity.exception.AppBusinessException;
import cn.sumpu.app.wlcity.util.Common;
import cn.sumpu.app.wlcity.web.util.ImageUtil;
import cn.sumpu.app.wlcity.web.util.PictureUrl;

@Controller
public class ContentController {
	
	private static Logger logger = LoggerFactory.getLogger(ContentController.class);
	@Autowired
	private ContentBiz contentBiz;
	
	@RequestMapping(value = "/contentList.cms")
	public String contentList(Integer page,HttpServletRequest request,String contentType,String radioApp) {
		try {
			if(page == null){
				page = 1;
			}
			if(contentType==null)
				contentType="01";
			if(radioApp==null)
				radioApp=Common.APP_VOTE;
			
			int count=contentBiz.getContentListCount(contentType,radioApp);
			int pageSize=Common.PAGE_SIZE_CONTENT; 
			int totalPage=(count+pageSize-1)/pageSize; 
			request.setAttribute("page", page); 
			request.setAttribute("totalPage", totalPage); 
			List<Content> contentList = contentBiz.getContentList(page,contentType,radioApp);
			request.setAttribute("contentList", contentList); 
			request.setAttribute("contentType", contentType);
			request.setAttribute("radioApp", radioApp);
		} catch (AppBusinessException e) {
			e.printStackTrace(); 
		}
		return "cms/content/contentList";
	}
	
	@RequestMapping(value = "/postContent.cms")
	public String postContent(Content content,HttpServletRequest request) {
		try {
			contentBiz.insertContent(content);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "cms/content/contentList";
	}
	@RequestMapping(value = "/getContentById.cms")
	public String getVoteById(HttpServletRequest request, String contentId,HttpServletResponse response) {
		try {
			response.setContentType("text/html; charset=UTF-8"); 
			PrintWriter pw = response.getWriter(); 
			Content content=contentBiz.getContentById(contentId); 
			pw.print(JSONArray.fromObject(content));
		} catch (Exception e) { 
			e.printStackTrace();
		}
		return null;
	}
	@RequestMapping(value = "/updateContent.cms")
	public String updateContent(HttpServletRequest request,HttpServletResponse response,Content content) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter pw = response.getWriter();  
			contentBiz.updateContent(content);
			pw.write("success");
		} catch (Exception e) { 
			e.printStackTrace();
		}
		return null;
	}
	// 上移
	@RequestMapping(value = "/addContentSort.cms")
	public String addContentSort(HttpServletRequest request,
			HttpServletResponse response, String contentId, Integer sort,String contentType) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter pw = response.getWriter();
			Content content = new Content();
			content.setIdentifier(contentId);
			content.setSort(sort);
			contentBiz.moveContentSortUp(content,contentType); 

			pw.write("success");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// 下移
	@RequestMapping(value = "/downContentSort.cms")
	public String downVoteSort(HttpServletRequest request,
			HttpServletResponse response, String contentId, Integer sort,String contentType) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter pw = response.getWriter();
			Content content = new Content();
			content.setIdentifier(contentId);
			content.setSort(sort);
			contentBiz.moveContentSortDown(content,contentType);
			pw.write("success");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	// 删除
	@RequestMapping(value = "/deleteContent.cms")
	public String deleteContent(HttpServletRequest request,
			HttpServletResponse response, String identifier,String contentType,Integer sort) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter pw = response.getWriter();
			contentBiz.deleteContent(identifier);
			contentBiz.downEachVoteSort(sort, contentType);
			pw.write("success");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	@RequestMapping(value = "/contentUploadImg.cms")
	public String contentUploadImg(HttpServletRequest request,HttpServletResponse response) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			PrintWriter pw = response.getWriter();
			PictureUrl pictureUrl = ImageUtil.uploadImage(request, "content");
			pw.print(JSONObject.fromObject(pictureUrl));
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("MVC failure.", e);
			}
		}
		return null;
	}

}
